package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.QuotaExpectedApplication;
import com.dhcc.DRGsHosp.core.domain.QuotaExpected;

@Named
@Transactional
public class QuotaExpectedApplicationImpl implements QuotaExpectedApplication {

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    @Autowired  
    private JdbcTemplateObj jdbcTemplateObj; 
    @Autowired
    private EntityRepository repository;
	private static final Logger LOGGER = LoggerFactory.getLogger(QuotaExpectedApplicationImpl.class);
    
	public QuotaExpected getQuotaExpected(Long id) {
		return QuotaExpected.get(QuotaExpected.class, id);
	}
	
	public void creatQuotaExpected(QuotaExpected quotaExpected) {
	try {
		quotaExpected.save();
	}catch(Exception e) {
		LOGGER.error(e.getMessage());
		System.err.println(quotaExpected.getExpectedId());
		e.printStackTrace();
	}
	}
	
	public void updateQuotaExpected(QuotaExpected quotaExpected) {/*
		String sql = "select 1 from drgs.t_quotaexpected where parexpected_code='"+quotaExpected.getParExpectedCode()+"' and parquota_code='"+quotaExpected.getParQuotaCode()+"' ";
		List<Object> list = repository.createSqlQuery(sql).list();
		if(list.size()>0){
			sql = "update drgs.t_quotaexpected set expected_value="+(quotaExpected.getExpectedValue()!=null?quotaExpected.getExpectedValue():0)+" where parexpected_code='"+quotaExpected.getParExpectedCode()+"' and parquota_code='"+quotaExpected.getParQuotaCode()+"' ";
		}else{
			sql = "insert into drgs.t_quotaexpected(parquota_code, parexpected_code, expected_value) values('"+quotaExpected.getParQuotaCode()+"', '"+quotaExpected.getParExpectedCode()+"', "+(quotaExpected.getExpectedValue()!=null?quotaExpected.getExpectedValue():0)+")";
		}
		repository.createSqlQuery(sql).executeUpdate();
	*/}
	
	public void removeQuotaExpected(QuotaExpected quotaExpected) {
		if(quotaExpected != null){
			quotaExpected.remove();
		}
	}
	
	public void removeQuotaExpecteds(Set<QuotaExpected> quotaExpecteds) {
		for (QuotaExpected quotaExpected : quotaExpecteds) {
			quotaExpected.remove();
		}
	}
	
	public List<QuotaExpected> findAllQuotaExpected() {
		return QuotaExpected.findAll(QuotaExpected.class);
	}

/**
 * 
 *  @Description：获取上次测算数据
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.QuotaExpectedApplication#getQuotaExpectedByCode(java.lang.String, java.lang.String, java.lang.String)
 */
	@Override
	public List<QuotaExpected> getQuotaExpectedByCode(String saveType, String objtype, String year,String roleName,String dateFrom,String dateTo) {
		StringBuilder sql = new StringBuilder(" select _quotaExpected from QuotaExpected _quotaExpected where objType='"+objtype+"' and expectedYear='"+year+"' and expectedCode='"+saveType+"' and roleName='"+roleName+"'  ");
		List<QuotaExpected> resultLs = getQueryChannelService().createJpqlQuery(sql.toString()).list();
		return resultLs;
	}
/**
 * 
 *  @Description：获取科室和科室组上次测试数据
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.QuotaExpectedApplication#getQuotaExpectedMapByCode(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public List<Map<String, Object>> getQuotaExpectedMapByCode(String saveType, String objtype, String year,String code,String roleName,String dateFrom,String dateTo) {
	String sql=" SELECT  t.parquota_code quota_code,t.expected_value quota_value  FROM drgs.t_quotaexpected t WHERE t.obj_type='"+objtype+"' AND t.expected_code='"+saveType+"' AND t.expected_year='"+year+"' AND  drgs.getSubStr(t.id,'_',0)='"+code+"' AND  drgs.getSubStr(t.id,'_',0)<>'hosp'  and role_name='"+roleName+"' ";
	 return jdbcTemplateObj.queryForList(sql) ;
}
/**
 * 
 *  @Description：获取编码
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.application.QuotaExpectedApplication#getCode(java.lang.String, java.lang.String, java.lang.String)
 */
@Override
public List<String> getCode(String saveType, String objtype, String year,String roleName) {
	String sql=" SELECT Distinct a.objcode FROM( \n" + 
			"SELECT drgs.getSubStr(t.id,'_',0) objcode FROM drgs.t_quotaexpected t WHERE t.obj_type='"+objtype+"' AND t.expected_code='"+saveType+"' AND t.expected_year='"+year+"'  and role_name='"+roleName+"'  \n" + 
			") a";
	return jdbcTemplateObj.queryForList(sql, String.class) ;
}
	
}
